package cn.com.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import cn.com.entity.User;

public class UserDao {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	Session session;
	Transaction transaction;
	public User zhuce(User u){
		session=sessionFactory.openSession();
		transaction=session.beginTransaction();
		session.save(u);
		transaction.commit();
		session.close();
		return u;
	}
	public User login(User u){
		User user=null;
		session=sessionFactory.openSession();
		String hql="from User where uname=? and upwd=?";
		Query query=session.createQuery(hql);
		query.setString(0, u.getUname());
		query.setString(1, u.getUpwd());
		List<User> list=query.list();
		if(list.size()==1){
			user=list.get(0);
		}
		session.close();
		return user;
		
	}
	public List<String> getUnames(){
		List<String> unames=new ArrayList<String>();
		session=sessionFactory.openSession();
		String hql="select u.uname from User u ";
		Query query=session.createQuery(hql);
		unames=query.list();
				
		return unames;
	}
}
